{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import pickle\n",
    "import numpy as np\n",
    "from scipy.optimize import curve_fit\n",
    "from scipy import asarray as ar,exp\n",
    "from sklearn import linear_model\n",
    "from lightgbm import LGBMRegressor \n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib notebook\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "infect_A=pd.read_csv('infectDataset/city_A/infection.csv',header=None)\n",
    "infect_A.columns = ['city','region_id','date', 'index']\n",
    "infect_B=pd.read_csv('infectDataset/city_B/infection.csv',header=None)\n",
    "infect_B.columns = ['city','region_id','date', 'index']\n",
    "infect_C=pd.read_csv('infectDataset/city_C/infection.csv',header=None)\n",
    "infect_C.columns = ['city','region_id','date', 'index']\n",
    "infect_D=pd.read_csv('infectDataset/city_D/infection.csv',header=None)\n",
    "infect_D.columns = ['city','region_id','date', 'index']\n",
    "infect_E=pd.read_csv('infectDataset/city_E/infection.csv',header=None)\n",
    "infect_E.columns = ['city','region_id','date', 'index']\n",
    "infect_F=pd.read_csv('infectDataset/city_F/infection.csv',header=None)\n",
    "infect_F.columns = ['city','region_id','date', 'index']\n",
    "infect_G=pd.read_csv('infectDataset/city_G/infection.csv',header=None)\n",
    "infect_G.columns = ['city','region_id','date', 'index']\n",
    "infect_H=pd.read_csv('infectDataset/city_H/infection.csv',header=None)\n",
    "infect_H.columns = ['city','region_id','date', 'index']\n",
    "infect_I=pd.read_csv('infectDataset/city_I/infection.csv',header=None)\n",
    "infect_I.columns = ['city','region_id','date', 'index']\n",
    "infect_J=pd.read_csv('infectDataset/city_J/infection.csv',header=None)\n",
    "infect_J.columns = ['city','region_id','date', 'index']\n",
    "infect_K=pd.read_csv('infectDataset/city_K/infection.csv',header=None)\n",
    "infect_K.columns = ['city','region_id','date', 'index']\n",
    "#\n",
    "infection_data={'A':infect_A,'B':infect_B,'C':infect_C,'D':infect_D,'E':infect_E,'F':infect_F,'G':infect_G,'H':infect_H,'I':infect_I,'J':infect_J,'K':infect_K}\n",
    "#按照date进行groupby,total_A就表示了city_A 45天每天的新增人数\n",
    "grouped=infect_A.groupby(['date'])\n",
    "total_A=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_B.groupby(['date'])\n",
    "total_B=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_C.groupby(['date'])\n",
    "total_C=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_D.groupby(['date'])\n",
    "total_D=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_E.groupby(['date'])\n",
    "total_E=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_F.groupby(['date'])\n",
    "total_F=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_G.groupby(['date'])\n",
    "total_G=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_H.groupby(['date'])\n",
    "total_H=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_I.groupby(['date'])\n",
    "total_I=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_J.groupby(['date'])\n",
    "total_J=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "grouped=infect_K.groupby(['date'])\n",
    "total_K=grouped['index'].agg(np.sum).reset_index(drop=True)\n",
    "city_infect={'A':total_A,'B':total_B,'C':total_C,'D':total_D,'E':total_E,'F':total_F,'G':total_G,'H':total_H,'I':total_I,'J':total_J,'K':total_K}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#这段代码\n",
    "   \n",
    "model_lgbm = LGBMRegressor(n_estimators=500, metric='mae', random_state=1234, min_child_samples=5, min_child_weight=0.000001)\n",
    "re_sub=pd.read_csv('submits/submissionShilft1_16.csv',header=None)\n",
    "re_sub.columns = ['city','region_id','date', 'index']\n",
    "pre_city_id={}\n",
    "pre_id={}\n",
    "def find_zero(data):\n",
    "    for i in range(len(data)):\n",
    "        if data[i]>0:\n",
    "            return i\n",
    "def calculate_lag(df, lag_list, column):\n",
    "    for lag in lag_list:\n",
    "        column_lag = column + \"_\" + str(lag)\n",
    "        df[column_lag] = df[column].shift(lag, fill_value=0)\n",
    "    return df\n",
    "\n",
    "def calculate_trend(df, lag_list, column):\n",
    "    for lag in lag_list:\n",
    "        trend_column_lag = \"Trend_\" + column + \"_\" + str(lag)\n",
    "        df[trend_column_lag] = (df[column].shift(0, fill_value=0) - \n",
    "                                df[column].shift(lag, fill_value=0))/df[column].shift(lag, fill_value=0.001)\n",
    "    return df\n",
    "\n",
    "def get_feature(df):\n",
    "    df = calculate_lag(df, range(1,7), 'index')\n",
    "    #all_data = calculate_lag(all_data, range(1,7), 'migration')\n",
    "    df = calculate_trend(df, range(1,7), 'index')\n",
    "    #all_data = calculate_trend(all_data, range(1,7), 'migration')\n",
    "    df.replace([np.inf, -np.inf], 0, inplace=True)\n",
    "    df.fillna(0, inplace=True)\n",
    "    return df\n",
    "def get_true(city,Id):\n",
    "    infection=infection_data[city]\n",
    "    id_infect=infection[infection['region_id'].isin([Id])].reset_index(drop=True)\n",
    "    return id_infect['index']\n",
    "grouped=re_sub.groupby(['city','region_id'])\n",
    "pre_dicSeir={'A':{},'B':{},'C':{},'D':{},'E':{},'F':{},'G':{},'H':{},'I':{},'J':{},'K':{}}\n",
    "for name,group in grouped:\n",
    "    pre_dic={}\n",
    "    data=group.reset_index(drop=True)\n",
    "    city=name[0]\n",
    "    Id=name[1]\n",
    "    true_data=get_true(city,Id)\n",
    "    cut=find_zero(true_data.values)\n",
    "    true_data=true_data[cut:].reset_index(drop=True)\n",
    "    y=data['index']\n",
    "    group=pd.concat((true_data,y),axis=0).reset_index()\n",
    "    \n",
    "    train_df=get_feature(group)\n",
    "    X_train=train_df.drop(['index'],axis=1)\n",
    "    Y_train=train_df['index']\n",
    "    regr = model_lgbm#\n",
    "    #regr= linear_model.LinearRegression()\n",
    "    regr.fit(X_train, Y_train)\n",
    "    y_pred = regr.predict(X_train)\n",
    "    fig, ax = plt.subplots(figsize=(10,6))\n",
    "    ax.plot(true_data, c='g', lw=3, label='True')\n",
    "    ax.plot(y_pred, c='b', lw=2, label='lgbFit')\n",
    "    x=[i+len(true_data) for i in range(30)]\n",
    "    ax.plot(x,y, c='r', lw=2, label='seir')\n",
    "    ax.set_xlabel('Day',fontsize=20)\n",
    "    ax.set_ylabel('Infect count', fontsize=20)\n",
    "    ax.grid(1)\n",
    "    plt.xticks(fontsize=20)\n",
    "    plt.yticks(fontsize=20)\n",
    "    plt.legend();\n",
    "    save_dir='SEIR_fit_city_lgbPersudo/'+city\n",
    "    if not os.path.exists(save_dir):\n",
    "        os.makedirs(save_dir)\n",
    "    plt.savefig(os.path.join(save_dir,str(Id)+'.png'))\n",
    "    plt.show()\n",
    "    plt.close()\n",
    "    pre_dicSeir[city][Id]=y_pred[-30:]\n",
    "    \n",
    "\n",
    "#for num,city,region_id,date,index in re_sub.itertuples():\n",
    "    #pre_id[region_id].append(index)\n",
    "    #re_sub1.loc[num,'index']=re_sub0.loc[num,'index']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "      <th>region_id</th>\n",
       "      <th>date</th>\n",
       "      <th>index</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>0</td>\n",
       "      <td>21200630</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>0</td>\n",
       "      <td>21200701</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  city  region_id      date  index\n",
       "0    A          0  21200630      3\n",
       "1    A          0  21200701      3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#生成提交结果\n",
    "import math\n",
    "import random\n",
    "submit=pd.read_csv('submission.csv',header=None)\n",
    "submit.columns = ['city','region_id','date', 'index']\n",
    "#\n",
    "submit=submit.drop(['index'],axis=1)\n",
    "pres=[]\n",
    "dic_pre=pre_dicSeir\n",
    "def convert_date(date):\n",
    "    if date==21200630:\n",
    "        return 0\n",
    "    else:\n",
    "        return date-21200700\n",
    "def get_result(city,region_id,date):\n",
    "    if city=='A':\n",
    "        pre=math.ceil(dic_pre['A'][region_id][convert_date(date)])\n",
    "    if city=='B':\n",
    "        pre=math.ceil(dic_pre['B'][region_id][convert_date(date)])\n",
    "    if city=='C':\n",
    "        pre=math.ceil(dic_pre['C'][region_id][convert_date(date)])\n",
    "    if city=='D':\n",
    "        pre=math.ceil(dic_pre['D'][region_id][convert_date(date)])\n",
    "    if city=='E':\n",
    "        pre=math.ceil(dic_pre['E'][region_id][convert_date(date)])\n",
    "    if city=='F':\n",
    "        pre=math.ceil(dic_pre['F'][region_id][convert_date(date)])\n",
    "    if city=='G':\n",
    "        pre=math.ceil(dic_pre['G'][region_id][convert_date(date)])\n",
    "    if city=='H':\n",
    "        pre=math.ceil(dic_pre['H'][region_id][convert_date(date)])\n",
    "    if city=='I':\n",
    "        pre=math.ceil(dic_pre['I'][region_id][convert_date(date)])\n",
    "    if city=='J':\n",
    "        pre=math.ceil(dic_pre['J'][region_id][convert_date(date)])\n",
    "    if city=='K':\n",
    "        pre=math.ceil(dic_pre['K'][region_id][convert_date(date)])\n",
    "    return pre\n",
    "#\n",
    "for _,city,region_id,date in submit.itertuples():\n",
    "    pre=get_result(city,region_id,date)\n",
    "    if pre<0:\n",
    "        pre=0\n",
    "    pres.append(int(pre))\n",
    "\n",
    "submit['index']=pres\n",
    "submit_dir='submits/'\n",
    "if not os.path.exists(submit_dir):\n",
    "    os.makedirs(submit_dir)\n",
    "submit.to_csv(os.path.join(submit_dir,'subLgbPersudo.csv'),index=False,header=False)\n",
    "submit.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "baidu",
   "language": "python",
   "name": "baidu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
